import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class MyMouseFrame extends JFrame implements MouseListener {
    public MyMouseFrame() {
        initJFrame();
    }

    private void initJFrame() {
        this.setSize(500, 500);
        this.setLocationRelativeTo(null);
        this.setLayout(null);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);

        JButton btn = new JButton("点我");
        btn.setBounds(0, 0, 100, 100);
//        匿名内部类实现事件监听
        btn.addMouseListener(this);
        this.getContentPane().add(btn);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        System.out.println("鼠标点击");
    }

    @Override
    public void mousePressed(MouseEvent e) {
        System.out.println("鼠标按下");
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        System.out.println("鼠标放开");
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        System.out.println("鼠标移入");
    }

    @Override
    public void mouseExited(MouseEvent e) {
        System.out.println("鼠标移出");
    }
}
